package managedbean.tourist;

import java.io.IOException;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import session.stateless.SystemMessagesBean;
import util.localization.WebLocalization;


@ManagedBean
@RequestScoped
public class TouristLogoutManagedBean implements Serializable {
    // EXCEPTION
    @EJB
    private SystemMessagesBean systemMessages;
    
    private FacesContext ctx = FacesContext.getCurrentInstance();
    private WebLocalization localization;
    
    //  user preferred locale
    private String preferredLocale;

    /** Creates a new instance of TouristLoginManagedBean */
    public TouristLogoutManagedBean() {
        /* LOCALIZATION */
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        /* END LOCALIZATION */
    }

    public void doLogout(ActionEvent event) {
        try {
            if (!ctx.getExternalContext().getSessionMap().isEmpty()) {
                ctx.getExternalContext().getSessionMap().clear();
                ctx.getExternalContext().invalidateSession();

                // create a session to store the server port and pass it over during the login of stakeholder and then destroy the session over at login
                ctx.getExternalContext().getSession(true);
                int serverPort = FacesContext.getCurrentInstance().getExternalContext().getRequestServerPort();
                FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("serverPort", serverPort);
                // redirection upon success clearing
                String serverName = FacesContext.getCurrentInstance().getExternalContext().getRequestServerName();
                // the project name /Tourism-war
                String contextPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
                FacesContext.getCurrentInstance().getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/index.xhtml");
            }
        }
        catch (IOException ex) {
            // EXCEPTION
            String statusMessage = systemMessages.getSystemMessage("REDIRECT_FAIL", preferredLocale);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, ""));
        }
    }

    public FacesContext getCtx() {
        return ctx;
    }

    public void setCtx(FacesContext ctx) {
        this.ctx = ctx;
    }

    public WebLocalization getLocalization() {
        return localization;
    }

    public void setLocalization(WebLocalization localization) {
        this.localization = localization;
    }

    public String getPreferredLocale() {
        return preferredLocale;
    }

    public void setPreferredLocale(String preferredLocale) {
        this.preferredLocale = preferredLocale;
    }

    public SystemMessagesBean getSystemMessages() {
        return systemMessages;
    }

    public void setSystemMessages(SystemMessagesBean systemMessages) {
        this.systemMessages = systemMessages;
    }
    
}
